package com.yhn.init.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhn.init.common.DeleteRequest;
import com.yhn.init.common.ErrorCode;
import com.yhn.init.exception.BusinessException;
import com.yhn.init.exception.ThrowUtils;
import com.yhn.init.mapper.OutboundItemsMapper;
import com.yhn.init.model.dto.outboundItems.OutboundItemsAddRequest;
import com.yhn.init.model.dto.outboundItems.OutboundItemsQueryRequest;
import com.yhn.init.model.dto.outboundItems.OutboundItemsUpdateRequest;
import com.yhn.init.model.entity.OutboundItems;
import com.yhn.init.model.vo.OutboundItemsVO;
import com.yhn.init.service.OutboundItemsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service("outboundItemsService")
public class OutboundItemsServiceImpl extends ServiceImpl<OutboundItemsMapper, OutboundItems> implements OutboundItemsService {

    @Override
    public Page<OutboundItemsVO> queryPage(OutboundItemsQueryRequest outboundItemsQueryRequest) {
        if (outboundItemsQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long current = outboundItemsQueryRequest.getCurrent();
        long size = outboundItemsQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<OutboundItems> outboundItemsPage = this.page(new Page<>(current, size),
                this.getQueryWrapper(outboundItemsQueryRequest));
        Page<OutboundItemsVO> outboundItemsVOPage = new Page<>(current, size, outboundItemsPage.getTotal());
        List<OutboundItemsVO> outboundItemsVO = this.getOutboundItemsVO(outboundItemsPage.getRecords());
        outboundItemsVOPage.setRecords(outboundItemsVO);
        return outboundItemsVOPage;
    }


    @Override
    public QueryWrapper<OutboundItems> getQueryWrapper(OutboundItemsQueryRequest outboundItemsQueryRequest) {
        QueryWrapper<OutboundItems> queryWrapper = new QueryWrapper<>();
        // 如果请求对象为空，直接返回一个空的查询包装器
        if (outboundItemsQueryRequest == null) {
            return queryWrapper;
        }
        // 从请求对象中获取各项查询条件
        Long id = outboundItemsQueryRequest.getId();
        queryWrapper.eq(id != null && id > 0, "id", id);
        return queryWrapper;
    }

    /**
     * 获取脱敏对象集合
     *
     * @param outboundItemsList 查询请求
     * @return 脱敏对象集合
     */
    @Override
    public List<OutboundItemsVO> getOutboundItemsVO(List<OutboundItems> outboundItemsList) {
        if (CollUtil.isEmpty(outboundItemsList)) {
            return new ArrayList<>();
        }
        return outboundItemsList.stream().map(this::getOutboundItemsVO).collect(Collectors.toList());
    }


    /**
     * 将对象转化为脱敏对象
     *
     * @param outboundItems 原对象
     * @return 脱敏对象
     */
    @Override
    public OutboundItemsVO getOutboundItemsVO(OutboundItems outboundItems) {
        if (outboundItems == null) {
            return null;
        }
        OutboundItemsVO outboundItemsVO = new OutboundItemsVO();
        BeanUtils.copyProperties(outboundItems, outboundItemsVO);
        return outboundItemsVO;
    }

    /**
     * 根据ID获取信息
     *
     * @param id id
     * @return 实体信息
     */
    @Override
    public OutboundItemsVO getInfoById(Long id) {
        OutboundItems outboundItems = this.getById(id);
        if (outboundItems == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "没有对应的信息");
        }
        return getOutboundItemsVO(outboundItems);
    }

    /**
     * 保存信息
     *
     * @param outboundItemsAddRequest 添加请求
     * @return 新生成的ID
     */
    @Override
    @Transactional
    public Long saveOutboundItems(OutboundItemsAddRequest outboundItemsAddRequest) {
        if (outboundItemsAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        OutboundItems outboundItems = new OutboundItems();
        BeanUtils.copyProperties(outboundItemsAddRequest, outboundItems);
        boolean result = this.save(outboundItems);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return outboundItems.getId();
    }


    /**
     * 修改信息
     *
     * @param outboundItemsUpdateRequest 修改请求
     * @return 是否修改成功
     */
    @Override
    @Transactional
    public Boolean updateOutboundItems(OutboundItemsUpdateRequest outboundItemsUpdateRequest) {
        if (outboundItemsUpdateRequest == null || outboundItemsUpdateRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        OutboundItems outboundItems = new OutboundItems();
        BeanUtils.copyProperties(outboundItemsUpdateRequest, outboundItems);
        long id = outboundItemsUpdateRequest.getId();
        // 判断是否存在
        OutboundItems oldOutboundItems = this.getById(id);
        ThrowUtils.throwIf(oldOutboundItems == null, ErrorCode.NOT_FOUND_ERROR);
        boolean result = this.updateById(oldOutboundItems);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return result;
    }

    /**
     * 删除信息
     *
     * @param deleteRequest 删除请求
     * @return 是否修改成功
     */
    @Override
    public Boolean deleteById(DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long id = deleteRequest.getId();
        // 判断是否存在
        boolean b = this.removeById(id);
        ThrowUtils.throwIf(!b, ErrorCode.NOT_FOUND_ERROR);
        return b;
    }


    /**
     * 批量删除
     *
     * @param ids 批量删除的IDS
     * @return 是否删除成功
     */
    @Override
    @Transactional
    public Boolean deleteBatchByIds(Long[] ids) {
        if (ids == null || ids.length == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = this.removeByIds(Arrays.asList(ids));
        ThrowUtils.throwIf(!b, ErrorCode.NOT_FOUND_ERROR);
        return b;
    }
}
